# -*- coding: utf-8 -*-
# 调用函数
# 利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串：
def testHex():
    print("\n----testHex----\n")
    n1 = 255
    n2 = 1000
    print("hex(n1) : "+str(hex(n1)))
    print("hex(n1) : "+str(hex(n2)))

# 定义函数
def my_abs(x):
    print("\n---my_abs----\n")
    if not isinstance(x,(int,float)):
        raise TypeError("bad operand type")
    else:
        if x<0:
            return -x
        else :
            return x

# 函数返回多个值
import math
def move(x,y,step,angle=0):
    print("\n---move----\n")
    nx = x+step*math.cos(angle)
    ny = y-step*math.sin(angle)
    return nx,ny

# 函数的参数：位置参数、默认参数
def power(x,n=2):
    print("\n-----power-----\n")
    s = 1
    while n > 0:
        s = x*s
        n = n-1
    return s
# 非可变参数
def calc(numbers):
    # numbers 为不可变类型turtle，*可以转为可变参数
    return calc2(*numbers)

# 可变参数 *
def calc2(*numbers):
    sum = 0
    for x in numbers:
        sum = sum + x*x
    return sum
# 关键参数 **
def person(name,age,**kw):
    return "name : %s age : %d kw : %s" % (name,age,str(kw))

# 命名参数+默认参数
def people(name,age,*,city ="广州",job):
    return "name : %s age : %d city : %s job : %s" % (name,age,city,job)
    

if __name__ == '__main__':
    testHex()
    print(my_abs(-1))
    print(move(100,100,60,math.pi/6))
    print("默认参数：%d"%power(5))
    print("位置参数:",power(5,2))
    print("不可变参数：",calc((1,2,3)))
    print("可变参数：",calc2(1,2,3))
    print("关键参数："+person("heihei",30,city='Beijing', addr='Chaoyang', zipcode=123456))
    print("命名参数："+people("heihei",30,job="程序员"))
